package com.benshou.bsoa.dto.yida.analysis;


import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.benshou.bsoa.constants.ProjectAnalysistFieldConstants.*;


/**
 * 将 ProjectAccount 对象转换为提交到钉钉宜搭表单所需的 JSON 格式（Map）。
 */
@Component
public class CovertProjectToFromDataJson {

    /**
     * 将项目账目信息封装成表单格式数据
     * @param account 项目账目信息对象
     * @return 表单数据的 Map 表示
     */
        public Map<String, Object> convertProjectAccountToFormData(ProjectAccount account) {
            Map<String, Object> formData = new HashMap<>();
            // 1. 项目状态
            formData.put(PROJECT_STATUS, account.getProjectStatus());
            // 2. 项目信息
            formData.put(PROJECT_NAME, account.getProjectName());
            formData.put(PROJECT_CODE, account.getProjectCode());
            formData.put(PROJECT_TYPE, account.getProjectType());
            // 3. 项目等级
            formData.put(PROJECT_LEVEL, account.getProjectLevel());
            // 4. 解决方案部门
            formData.put(SOLUTION_DEPT, account.getSolutionDeptIds());
            // 5. 项目经理
            formData.put(PROJECT_MANAGER, account.getUserIds());
            // 6. 项目时间
            formData.put(PLAN_DATES, account.getPlanDates());
            // 7. 合同金额
            formData.put(CONTRACT_AMOUNT, account.getContractAmount());
            // 8. 项目预算金额
            formData.put(BUDGET_COST, String.valueOf(account.getBudgetCost()));
            // 9. 人力成本明细
            List<Map<String, Object>> payrollList = new ArrayList<>();
            BigDecimal totalCost = BigDecimal.ZERO; // 项目总人力成本

            // 添加一条项目级别的薪资汇总记录
            Map<String, Object> one = new HashMap<>();
            one.put(PROJECT_SALARY_PROJECTNAME, account.getProjectName()); // 假设 account 有项目名
            one.put(PROJECT_SALARY_DATETIME, account.getMembers().get(0).getCostDate());
            one.put(PROJECT_SALARY_MEMBERS_TOTAL, account.getMembers().get(0).getTotalMembers());
            one.put(PROJECT_SALARY_PAY, account.getMembers().get(0).getSalaryCost());

            payrollList.add(one);
            formData.put(PROJECT_SALARY_TABLE_NAME, payrollList);

            // 10. 其他费用明细
            List<Map<String, Object>> others = new ArrayList<>();
            for (OtherCost costItem : account.getOtherCosts()) {
                Map<String, Object> two = new HashMap<>();
                two.put(OTHER_COST_TABLE_PRPJECT_NAME, costItem.getProjectName());
                two.put(OTHER_COST_TABLE_NAME, costItem.getCostName());
                two.put(OTHER_COST_TABLE_DATETIME, costItem.getCostDate());
                two.put(OTHER_COST_TABLE_MONEY, costItem.getCostAmount());

                others.add(two);
            }
            formData.put(OTHER_COST_TABLE, others);
            return formData;
        }
}
